package com.fh.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;


public class TestPay {
    //网关地址
    private final static String URL="https://openapi.alipaydev.com/gateway.do";
    //appid
    private final static String APPID = "2021000118676851";
    //应用私钥
    private final static String KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDRAmDBghxauyfK945t//T2/C3n8xN5t3Z17qynn2S6t4EHNsVhKfTRkJJqlf/Yj/toHTupbyQ2jgHYIEew90ii8e6T259C8UyBfcy+xJ3rADfZIiuBBrxWaHFcYolC1fHaEXotfXZdx0beUKOlQC1K9UpTRw2Ceyu00GdQWvuOBpB2fKLor8ZXT4Hztn0hGa+vNQjKt54YRLkyuGwumcKGgxFE7C3wdp+0CBQIEi4fgTEtWC7q2j5AUfb+VDF8OpBPD7/0zmq2hZkb1kXW/L0vnP4FRaqceJ+L6TGT1byK4IU/+m19usWlehrWSyebfHEUzi38UtS+ugYJhbTUfS8ZAgMBAAECggEAM5NOpPGiZZFBZIhqESLUJA5elZoZ+DMZfEiFOmfOx3R41nrhgfSEgSIXv018cMg21Z5BFrJiUUQBmH9+sWCUAAGLSdksF6zLoDiWyYxjG5ZWMbKI4bHzz66+Knn2PRPmNlWRvJ/1EXcXOczUVPbdNl4JFTu3TbdvNZMnq2CIKqdpuN8PPMROqJe/JiP27TdJwX5TgJR8HxAWzgmt9pQLdoW5N8ptWvhLVWodDdlS2HdFBlf1h8og/RSWVys1A+tpCapWR3r0OeC5Cisp+vUZ8Jr/wXKEB6eEa1nIh/xAoFjFsxbZrVhGL/YlDi+vZ5lxDB01XF3VzS+TsFhlEKBxMQKBgQDzLdaf6KI1ykCE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wGMxxV+Rl9XZuN76qc9VE0XCHkWmETZf1T3abuWYBBAHeCCaBb5WuFBnrtoTladIZwGLsMXaa1OWM3bVNw3c8Ug20ut7KooI9ymE+9SfC8UkgRdtnZKs3nyw5nyQJS4OmsKp/UuxmeZrBz3mADOfVHvp+aJt7N+UHSNzSZ4aaWFx3V+9IZ7a9j2rn+jKhoBrCe2P1ddCen2j2Q65wIDAQAB";


    public static void main(String[] args) throws AlipayApiException {
       /* String s = ceatePay("123456789", "iphone13 256G", 8888.88);
        System.out.println("付款码："+s);*/

        queyrStatu("123456789");
    }

        //生产二维码接口
    public static String ceatePay(String oid,String title,Double money) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(URL,APPID,KEY,"json","GBK",PUBLICKEY,"RSA2");
        //生成二维码支付的接口
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl("");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", oid);
        bizContent.put("total_amount", money);
        bizContent.put("subject", title);
        bizContent.put("timeout_express", "10m");
        request.setBizContent(bizContent.toString());
        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        System.out.println(JSONObject.toJSONString(response));
        return response.getQrCode();
    }
    //查看支付状态接口
    public static String queyrStatu(String oid) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(URL,APPID,KEY,"json","GBK",PUBLICKEY,"RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent("{" +
                "  \"out_trade_no\":\""+oid+"\"," +
                "  \"query_options\":[" +
                "    \"trade_settle_info\"" +
                "  ]" +
                "}");
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        String tradeStatus = response.getTradeStatus();
        System.out.println(tradeStatus);
        return tradeStatus;
    }
}
